/**
 * 
 */
package org.arpit.masteringextjs.rs;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.arpit.masteringextjs.dao.impl.ActorDAOImpl;
import org.arpit.masteringextjs.dao.impl.CategoryDAOImpl;
import org.arpit.masteringextjs.dao.impl.CityDAOImpl;
import org.arpit.masteringextjs.dao.impl.CountryDAOImpl;
import org.arpit.masteringextjs.dao.impl.LanguageDAOImpl;
import org.arpit.masteringextjs.domain.Actor;
import org.arpit.masteringextjs.domain.Category;
import org.arpit.masteringextjs.domain.City;
import org.arpit.masteringextjs.domain.Country;
import org.arpit.masteringextjs.domain.Language;
import org.arpit.masteringextjs.utility.DateTimeUtility;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * @author arpit
 * 
 */
@Path("/json/staticData")
public class StaticDataResource {
	
	@POST
	@Path("/create")
	@Produces(MediaType.APPLICATION_JSON)
	public String create(@FormParam("entity") String entity, @FormParam("data") JSONArray jsonArray) throws JSONException {
		final int length = jsonArray.length();
		switch (entity) {
		case "Actor":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final Actor actor = new Actor();
				actor.setFirstName(jsonObject.getString("first_name"));
				actor.setLastName(jsonObject.getString("last_name"));
				actor.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				ActorDAOImpl.getInstance().create(actor);
			}
			break;
		case "Category":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final Category category = new Category();
				category.setName(jsonObject.getString("name"));
				category.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				CategoryDAOImpl.getInstance().create(category);
			}
			break;
		case "City":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final City city = new City();
				city.setCity(jsonObject.getString("city"));
				city.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				CityDAOImpl.getInstance().create(city);
			}
			break;
		case "Country":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final Country country = new Country();
				country.setCountry(jsonObject.getString("country"));
				country.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				CountryDAOImpl.getInstance().create(country);
			}
			break;
		case "Language":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final Language language = new Language();
				language.setName(jsonObject.getString("name"));
				language.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				LanguageDAOImpl.getInstance().create(language);
			}
			break;
		default:
			throw new IllegalArgumentException("Illegal entity type : "
					+ entity);
		}
		
		return "{success : true}";
	}

	@POST
	@Path("/update")
	@Produces(MediaType.APPLICATION_JSON)
	public String update(@FormParam("entity") String entity, @FormParam("data") JSONArray jsonArray) throws JSONException {
		final int length = jsonArray.length();
		switch (entity) {
		case "Actor":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final Actor actor = new Actor();
				actor.setId(jsonObject.getLong("actor_id"));
				actor.setFirstName(jsonObject.getString("first_name"));
				actor.setLastName(jsonObject.getString("last_name"));
				actor.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				ActorDAOImpl.getInstance().update(actor);
			}
			break;
		case "Category":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final Category category = new Category();
				category.setId(jsonObject.getLong("category_id"));
				category.setName(jsonObject.getString("name"));
				category.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				CategoryDAOImpl.getInstance().update(category);
			}
			break;
		case "City":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final City city = new City();
				city.setId(jsonObject.getLong("city_id"));
				city.setCity(jsonObject.getString("city"));
				city.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				CityDAOImpl.getInstance().update(city);
			}
			break;
		case "Country":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final Country country = new Country();
				country.setId(jsonObject.getLong("country_id"));
				country.setCountry(jsonObject.getString("country"));
				country.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				CountryDAOImpl.getInstance().update(country);
			}
			break;
		case "Language":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				final Language language = new Language();
				language.setId(jsonObject.getLong("language_id"));
				language.setName(jsonObject.getString("name"));
				language.setLastUpdated(DateTimeUtility.toDate(jsonObject.getString("last_update")));
				LanguageDAOImpl.getInstance().update(language);
			}
			break;
		default:
			throw new IllegalArgumentException("Illegal entity type : "
					+ entity);
		}
		
		return "{success : true}";
	}

	@POST
	@Path("/findAll")
	@Produces(MediaType.APPLICATION_JSON)
	public String findAll(@FormParam("entity") String entity)
			throws JsonGenerationException, JsonMappingException, IOException {
		System.out.println("Inside StaticDataResource findAll for entity : "
				+ entity);

		ObjectMapper mapper = new ObjectMapper();
		String json = "";
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);

		switch (entity) {
		case "Actor":
			map.put("data", ActorDAOImpl.getInstance().findAll());
			break;
		case "Category":
			map.put("data", CategoryDAOImpl.getInstance().findAll());
			break;
		case "City":
			map.put("data", CityDAOImpl.getInstance().findAll());
			break;
		case "Country":
			map.put("data", CountryDAOImpl.getInstance().findAll());
			break;
		case "Language":
			map.put("data", LanguageDAOImpl.getInstance().findAll());
			break;
		default:
			throw new IllegalArgumentException("Illegal entity type : "
					+ entity);
		}

		// convert map to JSON string
		json = mapper.defaultPrettyPrintingWriter().writeValueAsString(map);
		return json;
	}
	
	@POST
	@Path("/delete")
	@Produces(MediaType.APPLICATION_JSON)
	public String delete(@FormParam("entity") String entity, @FormParam("data") JSONArray jsonArray) throws JSONException {
		final int length = jsonArray.length();
		switch (entity) {
		case "Actor":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				ActorDAOImpl.getInstance().delete(jsonObject.getLong("actor_id"));
			}
			break;
		case "Category":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				CategoryDAOImpl.getInstance().delete(jsonObject.getLong("category_id"));
			}
			break;
		case "City":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				CityDAOImpl.getInstance().delete(jsonObject.getLong("city_id"));
			}
			break;
		case "Country":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				CountryDAOImpl.getInstance().delete(jsonObject.getLong("country_id"));
			}
			break;
		case "Language":
			for (int i = 0; i < length; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				LanguageDAOImpl.getInstance().delete(jsonObject.getLong("language_id"));
			}
			break;
		default:
			throw new IllegalArgumentException("Illegal entity type : "
					+ entity);
		}
		
		return "{success : true}";
	}

}
